package pro.shushi.pamirs.hr.core.action.eam;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import pro.shushi.pamirs.hr.api.enums.BooleanConstantEnum;
import pro.shushi.pamirs.hr.api.enums.eam.MmdTypeEnum;
import pro.shushi.pamirs.hr.api.model.eam.BsMtoolMd;
import pro.shushi.pamirs.hr.api.model.eam.BsMtoolMdNr;
import pro.shushi.pamirs.hr.api.model.eam.BsMtoolMdStencil;
import pro.shushi.pamirs.hr.api.model.org.Employee;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.api.dto.common.Message;
import pro.shushi.pamirs.meta.api.session.PamirsSession;
import pro.shushi.pamirs.meta.constant.ExpConstants;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 工装型号自定义
 */
@Slf4j
@Component
@Model.model(BsMtoolMd.MODEL_MODEL)
public class BsMtoolMdAction {

    /**
     * 自定义构造方法示例
     * 如：界面设计器提交数据-选择的提交函数，新增时系统会调用construct
     * @param data
     * @return
     */
    @Function(openLevel = FunctionOpenEnum.API)
    @Function.Advanced(type = FunctionTypeEnum.QUERY, displayName = "复制")
    public BsMtoolMd copy(BsMtoolMd data) {
        if (data.getId() != null) {
            data = data.queryById();
            data.unsetId();
            data.unsetCreateDate();
            data.unsetWriteDate();
            data.unsetCreateUid();
            data.unsetWriteUid();
            data.unsetMmdCode();
        }

        return data;
    }


    @Action.Advanced(name = FunctionConstants.create, managed = true, invisible = ExpConstants.idValueExist)
    @Action(displayName = "保存", summary = "添加", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public BsMtoolMd create(BsMtoolMd data) {

        data.construct();
        data.create();

        //新增编辑平行表
        addOrUpdateParallel(data);

        return data;
    }

    // 重写默认的更新保存函数(如果默认满足则不需要重写)
    @Transactional
    @Function.Advanced(type = FunctionTypeEnum.UPDATE)
    @Action.Advanced(name = FunctionConstants.update, managed = true, invisible = ExpConstants.idValueNotExist)
    @Action(displayName = "更新", summary = "修改", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.update)
    @Function.fun(FunctionConstants.update)
    public BsMtoolMd update(BsMtoolMd data) {

        data.updateById();
        //新增编辑平行表
        addOrUpdateParallel(data);

        return data;
    }


    /**
     * 新增修改平行表
     */
    public BsMtoolMd addOrUpdateParallel(BsMtoolMd data) {
        //获取特殊分类，模治具分类新增编辑模治具属性，钢网新增编辑钢网属性
        if(MmdTypeEnum.MTOOL.equals(data.getMmdType())){
            if(data.getLoadProdQty() !=null ){
                //新增编辑模治具属性平行表
                addOrUpdateBsMtoolMdNr(data);
            }
            data.getBsMtoolMdStencil().deleteByUnique();
        } else if(MmdTypeEnum.STENCIL.equals(data.getMmdType())){
            if(data.getStdMinTension() != null){
                //新增编辑钢网属性平行表
                addOrUpdateBsMtoolMdStencil(data);
            }
            data.getBsMtoolMdNr().deleteByUnique();
        }

        return data;
    }

    /**
     * 新增编辑BsMtoolMdNr
     */
    private void addOrUpdateBsMtoolMdNr(BsMtoolMd data){
        String orgCode = data.getOrgCode();
        String mmdCode = data.getMmdCode();
        BsMtoolMdNr mdNrRow = new BsMtoolMdNr().setOrgCode(orgCode).setMmdCode(mmdCode).queryOne();
        if(mdNrRow == null){
            //新增
            mdNrRow = new BsMtoolMdNr();
            mdNrRow.setOrgCode(data.getOrgCode());
            mdNrRow.setMmdCode(data.getMmdCode());
            mdNrRow.setLoadProdQty(data.getLoadProdQty());
            mdNrRow.create();

        } else {
            //编辑
            mdNrRow.setLoadProdQty(data.getLoadProdQty());
            mdNrRow.updateById();
        }
    }

    /**
     * 新增编辑BsMtoolMdStencil
     */
    private void addOrUpdateBsMtoolMdStencil(BsMtoolMd data){
        String orgCode = data.getOrgCode();
        String mmdCode = data.getMmdCode();
        BsMtoolMdStencil mdStencilRow = new BsMtoolMdStencil().setOrgCode(orgCode).setMmdCode(mmdCode).queryOne();
        if(mdStencilRow == null){
            //新增
            mdStencilRow = new BsMtoolMdStencil();
            mdStencilRow.setOrgCode(data.getOrgCode());
            mdStencilRow.setMmdCode(data.getMmdCode());
            mdStencilRow.setStdMinTension(data.getStdMinTension());
            mdStencilRow.setStdMaxTension(data.getStdMaxTension());
            mdStencilRow.setStdAngleMax(data.getStdAngleMax());
            mdStencilRow.setStdAllDiff(data.getStdAllDiff());
            mdStencilRow.setMdcOnTime(data.getMdcOnTime());
            mdStencilRow.create();

        } else {
            //编辑
            mdStencilRow.setStdMinTension(data.getStdMinTension());
            mdStencilRow.setStdMaxTension(data.getStdMaxTension());
            mdStencilRow.setStdAngleMax(data.getStdAngleMax());
            mdStencilRow.setStdAllDiff(data.getStdAllDiff());
            mdStencilRow.setMdcOnTime(data.getMdcOnTime());
            mdStencilRow.updateById();
        }
    }


    /**
     * 测试批量生产大量数据以测试导出性能
     * @param dataList
     * @return
     */
//    @Action(displayName = "生成大量数据",bindingType = ViewTypeEnum.TABLE,contextType = ActionContextTypeEnum.SINGLE_AND_BATCH)
//    @Transactional
//    public BsMtoolMd message(List<BsMtoolMd> dataList) {
//        int size = 100000;
////        int size = 1;
//        BsMtoolMd data = dataList.get(0);
//        for(int i=0;i<size;i++){
//            BsMtoolMd insertRow = new BsMtoolMd();
//
//            insertRow.setOrgCode("001");
//            insertRow.setMmdCode("BATM0"+i);
//            insertRow.setMmdName("名M0"+i);
//            insertRow.setIsValid(BooleanConstantEnum.Y);
//            insertRow.create();
//        }
//        return data;
//    }






}
